package com.ruoyi.web.task;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ruoyi.common.utils.AliyunSmsUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SpokenTeacher;
import com.ruoyi.system.domain.SpokenUser;
import com.ruoyi.system.domain.SpokenUserCourseTime;
import com.ruoyi.system.service.ISpokenTeacherService;
import com.ruoyi.system.service.ISpokenUserCourseTimeService;
import com.ruoyi.system.service.ISpokenUserService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Component("GoToClassSmsNotify")
public class GoToClassSmsNotify {

    @Autowired
    ISpokenUserCourseTimeService userCourseTimeService;

    @Autowired
    ISpokenUserService spokenUserService;

    @Autowired
    ISpokenTeacherService teacherService;

    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");

    public void executeGoToClass(){
        String today = LocalDate.now().format(formatDate);
        List<SpokenUserCourseTime> spokenUserCourseTimes = userCourseTimeService.select2HoursFromClass(today);

        for (SpokenUserCourseTime spokenUserCourseTime : spokenUserCourseTimes) {
            String datetime = spokenUserCourseTime.getChooseDate().format(formatDate) + " " +  spokenUserCourseTime.getCourseTime().getStartTime().format(formatTime);
            Long userId = spokenUserCourseTime.getUserId();
            SpokenUser user = spokenUserService.selectById(userId);
            String nickName = user.getSysUser().getNickName();
            String phonenumber = user.getSysUser().getPhonenumber();
            if(StringUtils.isEmpty(phonenumber)){
                continue;
            }
            Long teacherId = spokenUserCourseTime.getTeacherId();
            SpokenTeacher spokenTeacher = teacherService.selectById(teacherId);
            String teacherName = spokenTeacher.getSysUser().getNickName();
            String meetingType = StringUtils.isEmpty(spokenTeacher.getMeetingType()) ? "暂无" : spokenTeacher.getMeetingType();
            String meetingCode = StringUtils.isEmpty(spokenTeacher.getMeetingCode()) ? "暂无" : spokenTeacher.getMeetingCode();

            String templateCode = aliyunSmsUtils.getGoClassTemplateCode();
            String param = "{\"studentname\":\""+ nickName +"\", \"time\": \""+ datetime +"\", \"teachername\": \"" + teacherName + "\", \"meetingplat\": \""+ meetingType + "\", \"meetingid\": \""+ meetingCode + "\"}";
            SendSmsResponse sendSmsResponse = aliyunSmsUtils.sendMessage(aliyunSmsUtils.getSignName(), templateCode, phonenumber, param);
            // 发送成功
            if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                System.out.println(phonenumber + "的上课通知成功");
                // 修改上课通知状态
                SpokenUserCourseTime newUserCourseTime = new SpokenUserCourseTime();
                newUserCourseTime.setClassNotifyStatus(1);
                newUserCourseTime.setUctId(spokenUserCourseTime.getUctId());
                userCourseTimeService.update(newUserCourseTime);
            }
        }

    }

}
